package com.od.controller;

import com.od.entity.Category;
import com.od.service.CategoryService;
import com.od.utils.CategoryJson;
import com.od.utils.IndustryCategory;
import com.od.utils.IndustryJson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("allCategories")
    @ResponseBody
    public List<Category> allCategories() {
        return categoryService.getAllCategories();
    }

    @RequestMapping("allIndustryCategory")
    @ResponseBody
    public List<IndustryJson> allIndustryCategory() {
        List<IndustryJson> industryJsons = new ArrayList<>();
        List<IndustryCategory> industryCategories = categoryService.getIndustryCategory();
        for (IndustryCategory industryCategory : industryCategories) {
            IndustryJson industryJson = new IndustryJson();
            industryJson.setValue(industryCategory.getIndustryId());
            industryJson.setLabel(industryCategory.getIndustryName());
            List<CategoryJson> categoryJsons = new ArrayList<>();
            for (Category category : industryCategory.getCategories()) {
                CategoryJson categoryJson = new CategoryJson(category.getCategoryId(), category.getCategoryName());
                categoryJsons.add(categoryJson);
            }
            industryJson.setChildren(categoryJsons);
            industryJsons.add(industryJson);
        }
        return industryJsons;
    }

    @RequestMapping("getCategoryNameById")
    @ResponseBody
    public String getCategoryNameById(int categoryId) {
        return categoryService.getCategoryNameById(categoryId);
    }

}
